package cn.aitstone.tran;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

public class Producer {

    public static void main(String[] args) throws Exception {
        // 创建生产者
        TransactionMQProducer producer = new TransactionMQProducer("producer_hello_group");
        // 连接nameserver
        producer.setNamesrvAddr("127.0.0.1:9876");
        // 指定事务监听器
        producer.setTransactionListener(new MyTranListener());
        // 启动生产者
        producer.start();
        // 创建消息
        Message message = new Message("hello_topic", "hello_tags", "我已经学习4个多月了".getBytes(StandardCharsets.UTF_8));
        // 发送消息
        TransactionSendResult sendResult = producer.sendMessageInTransaction(message, null);
        System.out.println(sendResult);
        // 关闭生产者
        producer.shutdown();

    }
}
